<?php
import( 'tools.Setup.interfaces.SetupScreen' );
import( 'tools.Setup.classes.DatabaseSetupScreen' );
class ProjectScreen extends DatabaseSetupScreen implements SetupScreen
{
	public function __construct( $config, $words, $name, $path )
	{
		parent::__construct( $config, $words, $name, $path );
		$this->connectDatabase( parse_ini_file( $this->config['sources']['db_access'] ) );
	}

	public function handleAction( $action )
	{
		if( $action == "saveProject" )
		{
			if( !$_POST['title'] )
				throw new Exception( 'Project Title is not defined.' );
			if( !$_POST['description'] )
				throw new Exception( 'Project Description is not defined.' );

			$query	= "UPDATE {#prefix#}projects SET title=\"".$_POST['title']."\", description=\"".$_POST['description']."\" WHERE project_id=1";
			if( !$this->queryDatabase( $query ) )
				throw new Exception( "Project could not been updated." );
			return true;
		}
	}

	public function getView()
	{
		$query	= "SELECT * FROM {#prefix#}projects WHERE project_id=1";
		$result	= $this->queryDatabase( $query );
		$data	= mysql_fetch_array( $result );

		if( $this->config['fields'] )
		{
			foreach( $this->config['fields'] as $fieldName => $fieldDefinition )
			{
				$ui["label_".$fieldName]	= "<label for='".$fieldName."'>".$this->words['fields'][$fieldName]."</label>";
				if( $fieldDefinition['type'] == "input" )
				{
					$ui["field_".$fieldName]	= Elements::Input( $fieldName, $data[$fieldName] );
				}
			}
		}
		return $this->loadTemplate( $ui );
	}
}
?>